package mortal.game.control
{
    import Message.Db.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import fl.data.*;
    import flash.events.*;
    import modules.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.view.autoFight.*;
    import mortal.game.view.group.*;
    import mortal.game.view.system.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GroupController extends Controller
    {
        private var groupModule:GroupModuleNew;
        private var groupHintIcon:GroupHintIcon;
        public static const Type_invite:int = 0;
        public static const Type_apply:int = 1;

        public function GroupController()
        {
            
        }

        public function get hintIcon() : GroupHintIcon
        {
            if (!this.groupHintIcon)
            {
                this.groupHintIcon = new GroupHintIcon();
            }
            return this.groupHintIcon;
        }

        override protected function initView() : IView
        {
            if (!this.groupModule)
            {
                this.groupModule = new GroupModuleNew();
                this.groupModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            }
            return this.groupModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.Group_DoApply, this.doApply);
            Dispatcher.addEventListener(EventName.Group_DoInvite, this.doInvite);
            Dispatcher.addEventListener(EventName.Group_OpenWin, this.openWin);
            Dispatcher.addEventListener(EventName.Group_DoKickOut, this.doKickout);
            Dispatcher.addEventListener(EventName.Group_DoLeaveGroup, this.leaveGroup);
            Dispatcher.addEventListener(EventName.Group_DoChangeCaptain, this.doChangeCaptain);
            Dispatcher.addEventListener(EventName.SetAutoFightSuccess, this.onAutoFightSetChange);
            NetDispatcher.addCmdListener(ServerCommand.Group_Apply, this.onApply);
            NetDispatcher.addCmdListener(ServerCommand.Group_Invite, this.onInvite);
            NetDispatcher.addCmdListener(ServerCommand.Group_PlayerInfoChange, this.onPlayerInfoChange);
            NetDispatcher.addCmdListener(ServerCommand.Group_CaptainChange, this.onPlayerInfoChange);
            NetDispatcher.addCmdListener(ServerCommand.Group_GroupSetting, this.onGroupSettingChange);
            
        }

        private function onAddToStage(event:Event) : void
        {
            this.onPlayerInfoChange();
            this.groupModule.refreshNearbyList();
            if (cache.group.isInGroup)
            {
                this.groupModule.nearbyTabBarSelectedIndex = 1;
            }
            else
            {
                this.groupModule.nearbyTabBarSelectedIndex = 0;
            }
            this.groupModule.addEventListener(GroupModuleNew.Event_KickOut, this.doKickout);
            this.groupModule.addEventListener(GroupModuleNew.Event_LeaveGroup, this.leaveGroup);
            
        }

        private function openWin(event:Event) : void
        {
            var inviteOrApply:int;
            var data:*;
            var alertCloseHandler:Function;
            var gi:SGroupInvite;
            var gie:SGroupInviteEx;
            var captain:SPublicMiniPlayer;
            var copy:TCopy;
            var gi2:SGroupInvite;
            var e:* = event;
            alertCloseHandler = function (param1:int) : void
            {
                if (param1 == Alert.YES)
                {
                    doAgree(inviteOrApply, data);
                }
                else if (param1 == Alert.NO)
                {
                    doReject(inviteOrApply, data);
                }
                hintIcon.updateNum();
                
            }
            ;
            var desStr:String;
            var group:* = Cache.instance.group;
            if (group.inviteList.length > 0)
            {
                inviteOrApply = Type_invite;
                data = group.inviteList[0];
                if (data is SGroupInvite)
                {
                    gi = data as SGroupInvite;
                    desStr = HTMLUtil.addColor(gi.player.name, ColorConfig.instance.getCampColor(gi.player.camp).color) + "邀请你加入队伍，是否接受？";
                }
                else if (data is SGroupInviteEx)
                {
                    gie = data as SGroupInviteEx;
                    captain = this.getCaptain(gie.groupInfo);
                    if (gie.copyCode == 0)
                    {
                        desStr = HTMLUtil.addColor(captain.name, ColorConfig.instance.getCampColor(captain.camp).color) + "邀请你加入队伍，是否接受？";
                    }
                    else
                    {
                        copy = CopyConfig.instance.getInfoByCode(gie.copyCode);
                        desStr = "    " + HTMLUtil.addColor(captain.name, ColorConfig.instance.getCampColor(captain.camp).color) + "邀请你加入队伍进行" + copy.name + "副本，\n你确认要加入队伍吗？";
                    }
                }
            }
            else if (group.applyList.length > 0)
            {
                inviteOrApply = Type_apply;
                data = group.applyList[0];
                gi2 = data as SGroupInvite;
                desStr = HTMLUtil.addColor(gi2.player.name, ColorConfig.instance.getCampColor(gi2.player.camp).color) + "申请加入队伍，是否接受？";
            }
            else
            {
                
            }
            Alert.show(desStr, "组队请求", Alert.YES | Alert.NO, null, alertCloseHandler);
            
        }

        private function getCaptain(param1:SGroup) : SPublicMiniPlayer
        {
            var _loc_2:int = 0;
            while (_loc_2 < param1.players.length)
            {
                
                if (EntityUtil.toString(param1.captainId) == EntityUtil.toString((param1.players[_loc_2] as SPublicMiniPlayer).entityId))
                {
                    return param1.players[_loc_2] as SPublicMiniPlayer;
                }
                _loc_2++;
            }
            return null;
        }

        private function onPlayerInfoChange(param1:Object = null) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:Array = null;
            var _loc_4:DataProvider = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:SPublicMiniPlayer = null;
            var _loc_8:Boolean = false;
            var _loc_9:Boolean = false;
            if (this.groupModule)
            {
                _loc_2 = cache.group.isCaptain;
                this.groupModule.isCaptain = _loc_2;
                _loc_3 = cache.group.players;
                _loc_4 = new DataProvider();
                _loc_5 = [];
                _loc_6 = 0;
                while (_loc_6 < _loc_3.length)
                {
                    
                    _loc_7 = _loc_3[_loc_6] as SPublicMiniPlayer;
                    _loc_8 = _loc_7.entityId.id == cache.role.entityInfo.entityId.id;
                    _loc_9 = cache.group.captain && cache.group.captain.id == _loc_7.entityId.id;
                    _loc_4.addItem({type:"", online:_loc_7.online, isCaption:_loc_9, captainMode:_loc_2, isMySelf:_loc_8, player:_loc_7});
                    _loc_6++;
                }
                _loc_4.sortOn(["isCaption", "online"], Array.DESCENDING);
                this.groupModule.updateGroupList(_loc_4);
                this.groupModule.refreshNearbyList();
                if (cache.group.players.length > 0)
                {
                    cache.group.inviteList = [];
                    this.updateInviteData();
                }
                if (cache.group.players.length > 0 && cache.group.captain.id != cache.role.entityInfo.entityId.id)
                {
                    cache.group.applyList = [];
                    this.updateApplyData();
                }
            }
            
        }

        private function onGroupSettingChange(param1:SGroupSetting) : void
        {
            if (this.groupModule)
            {
                this.groupModule.updateGroupSetStatus();
            }
            
        }

        private function updateInviteData() : void
        {
            var _loc_1:* = cache.group.inviteList;
            var _loc_2:* = new DataProvider();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_2.addItem({type:"", data:_loc_1[_loc_3]});
                _loc_3++;
            }
            this.hintIcon.updateNum();
            
        }

        private function updateApplyData() : void
        {
            var _loc_1:* = cache.group.applyList;
            var _loc_2:* = new DataProvider();
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_2.addItem({type:"", data:_loc_1[_loc_3]});
                _loc_3++;
            }
            this.hintIcon.updateNum();
            
        }

        private function onInvite(param1:Object) : void
        {
            var _loc_2:SGroupInvite = null;
            var _loc_3:SGroupInviteEx = null;
            if (SystemSetter.currentSetter.refuseTeam)
            {
                if (param1 is SGroupInvite)
                {
                    _loc_2 = param1 as SGroupInvite;
                    GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeInvite, _loc_2.player.entityId, _loc_2.certId);
                }
                else if (param1 is SGroupInviteEx)
                {
                    _loc_3 = param1 as SGroupInviteEx;
                    GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeInvite, _loc_3.groupInfo.captainId, _loc_3.certId);
                }
                cache.group.removeFromInviteList(param1);
                
            }
            if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.autoAcceptTeam)
            {
                if (param1 is SGroupInvite)
                {
                    _loc_2 = param1 as SGroupInvite;
                    GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeInvite, _loc_2.player.entityId, _loc_2.certId);
                }
                else if (param1 is SGroupInviteEx)
                {
                    _loc_3 = param1 as SGroupInviteEx;
                    GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeInvite, _loc_3.groupInfo.captainId, _loc_3.certId);
                }
                cache.group.removeFromInviteList(param1);
                
            }
            this.hintIcon.updateNum();
            UiHintIconView.instance.addHintIcon(this.hintIcon);
            this.updateInviteData();
            
        }

        private function onApply(param1:Object) : void
        {
            var _loc_2:SGroupInvite = null;
            if (SystemSetter.currentSetter.refuseTeam && cache.group.players.length == 0)
            {
                _loc_2 = param1 as SGroupInvite;
                GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeApply, _loc_2.player.entityId, _loc_2.certId);
                cache.group.removeFromApplyList(_loc_2);
                
            }
            if (AutoFight.currentAutoFight.isAutoFight && AutoFight.currentAutoFight.autoAcceptTeam)
            {
                _loc_2 = param1 as SGroupInvite;
                GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeApply, _loc_2.player.entityId, _loc_2.certId);
                cache.group.removeFromApplyList(_loc_2);
                
            }
            this.hintIcon.updateNum();
            UiHintIconView.instance.addHintIcon(this.hintIcon);
            this.updateApplyData();
            
        }

        private function doChangeCaptain(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPublicMiniPlayer;
            GameProxy.groupProxy.modifyCaptain(_loc_2.entityId);
            
        }

        private function onAutoFightSetChange(event:DataEvent) : void
        {
            if (cache.group.isCaptain && cache.group.autoEnter != AutoFight.localAutoFight.autoAcceptTeam)
            {
                cache.group.autoEnter = AutoFight.localAutoFight.autoAcceptTeam;
                if (this.groupModule)
                {
                    this.groupModule.updateGroupSetStatus();
                    this.groupModule.updateGroupSet();
                }
            }
            
        }

        private function doApply(event:DataEvent) : void
        {
            var _loc_2:SEntityId = null;
            if (Game.sceneInfo.sMapDefine.instanceType.value() == EMapInstanceType._EMapInstanceTypeBattlefield)
            {
                MsgManager.addBroadCast("你正在战场中，不能申请入队！");
            }
            else
            {
                _loc_2 = event.data as SEntityId;
                if (_loc_2.id == cache.role.entityInfo.entityId.id)
                {
                    MsgManager.addBroadCast("不能向自己发送申请");
                }
                else
                {
                    GameProxy.groupProxy.apply(_loc_2);
                }
            }
            
        }

        private function doInvite(event:DataEvent) : void
        {
            var _loc_2:SEntityId = null;
            if (Game.sceneInfo.sMapDefine.instanceType.value() == EMapInstanceType._EMapInstanceTypeBattlefield)
            {
                MsgManager.addBroadCast("你正在战场中，不能邀请组队！");
            }
            else
            {
                _loc_2 = event.data as SEntityId;
                if (cache.group.players.length >= 5)
                {
                    MsgManager.addBroadCast("队伍已满，无法加入更多成员");
                }
                else if (_loc_2.id == cache.role.entityInfo.entityId.id)
                {
                    MsgManager.addBroadCast("不能邀请自己");
                }
                else
                {
                    GameProxy.groupProxy.invite(_loc_2);
                }
            }
            
        }

        private function doAgree(param1:int, param2) : void
        {
            var _loc_3:SGroupInvite = null;
            var _loc_4:SGroupInviteEx = null;
            if (param1 == Type_apply)
            {
                if (cache.group.players.length >= 5)
                {
                    MsgManager.addBroadCast("队伍已满");
                }
                else
                {
                    _loc_3 = param2 as SGroupInvite;
                    GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeApply, _loc_3.player.entityId, _loc_3.certId);
                    cache.group.removeFromApplyList(_loc_3);
                    this.updateApplyData();
                }
            }
            else
            {
                if (param2 is SGroupInvite)
                {
                    if (cache.group.players.length >= 5)
                    {
                        MsgManager.addBroadCast("队伍已满");
                    }
                    else
                    {
                        _loc_3 = param2 as SGroupInvite;
                        GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeInvite, _loc_3.player.entityId, _loc_3.certId);
                        cache.group.removeFromInviteList(_loc_3);
                    }
                }
                else if (cache.group.players.length >= 5)
                {
                    MsgManager.addBroadCast("队伍已满");
                }
                else
                {
                    _loc_4 = param2 as SGroupInviteEx;
                    GameProxy.groupProxy.agree(EGroupMsgType.EGroupMsgTypeInvite, _loc_4.groupInfo.captainId, _loc_4.certId);
                    cache.group.removeFromInviteList(_loc_4);
                }
                this.updateInviteData();
            }
            this.hintIcon.updateNum();
            
        }

        private function doReject(param1:int, param2) : void
        {
            var _loc_3:SGroupInvite = null;
            var _loc_4:SGroupInviteEx = null;
            if (param1 == Type_apply)
            {
                _loc_3 = param2 as SGroupInvite;
                cache.group.removeFromApplyList(_loc_3);
                GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeApply, _loc_3.player.entityId, _loc_3.certId);
                this.updateApplyData();
            }
            else
            {
                if (param2 is SGroupInvite)
                {
                    _loc_3 = param2 as SGroupInvite;
                    cache.group.removeFromInviteList(_loc_3);
                    GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeInvite, _loc_3.player.entityId, _loc_3.certId);
                }
                else
                {
                    _loc_4 = param2 as SGroupInviteEx;
                    cache.group.removeFromInviteList(_loc_4);
                    GameProxy.groupProxy.reject_async(EGroupMsgType.EGroupMsgTypeInvite, _loc_4.groupInfo.captainId, _loc_4.certId);
                }
                this.updateInviteData();
            }
            this.hintIcon.updateNum();
            
        }

        private function leaveGroup(event:DataEvent) : void
        {
            var e:* = event;
            if (cache.copy.isInCopy && cache.copy.copyInfo && cache.copy.copyInfo.singleMode != 1)
            {
                Alert.show("退出队伍将会被传送出副本，你确定退出吗？", "提示", Alert.OK | Alert.CANCEL, null, function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    doLeaveGroup();
                }
                
            }
            );
            }
            else
            {
                this.doLeaveGroup();
            }
            
        }

        private function doLeaveGroup() : void
        {
            GameProxy.groupProxy.left();
            cache.group.players = [];
            this.onPlayerInfoChange();
            
        }

        private function doKickout(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            GameProxy.groupProxy.kickOut(_loc_2);
            
        }

        private function openNearbyList(event:DataEvent) : void
        {
            GameController.nearbyList.view.show();
            
        }

    }
}
